#include "kernel/types.h"  
#include "user/user.h"  
  
int main(int argc, char *argv[]) {  
  int pid;  
  int pipes1[2], pipes2[2]; 
  //parent send in pipes1[1], child receives in pipes1[0]
  //child send in pipes2[1], parent receives in pipes2[0]
  char buf[1]; 
  
  if (pipe(pipes1) == -1 || pipe(pipes2) == -1) {  
    exit(1);  
  }  
  
  pid = fork();  
  
  if (pid == -1)  exit(1);  
  
  if (pid == 0) {  
    // Child process  
    close(pipes1[1]); // Close unused write end of pipe1  
    close(pipes2[0]); // Close unused read end of pipe2  
  
    if (read(pipes1[0], buf, 1) == -1) exit(1);   
  
    printf("%d: received ping\n", getpid());     
    exit(0);  
  } else {  
    // Parent process  
    close(pipes1[0]); // Close unused read end of pipe1  
    close(pipes2[1]); // Close unused write end of pipe2  
  
    if (write(pipes1[1], buf, 1) == -1) exit(1); 
  
    if (read(pipes2[0], buf, 1) == -1) exit(1);
  
    printf("%d: received pong\n", getpid());  
    exit(0);  
  }  
}